<script setup lang="ts">
import { useMainStore } from "./store";
import { onMounted } from "vue";
import * as echarts from 'echarts'
import './assets/china.js'
import { geoCoordMap } from './assets/geoMap'

const main = useMainStore()

onMounted(async () => {
  await main.getList()
  initCharts()
  initPie()
  initLine()
})

const initCharts = () => {
  const city = main.list.diseaseh5Shelf.areaTree[0].children
  main.item = city[5].children
  const data = city.map((item) => {
    return {
      name: item.name,
      value: geoCoordMap[item.name].concat(item.total.nowConfirm),
      children: item.children
    }
  })
  const charts = echarts.init(document.querySelector('#China') as HTMLElement)
  charts.setOption({
    // backgroundColor: "black",
    geo: {
      map: "china",
      aspectScale: 0.8,
      layoutCenter: ["50%", "50%"],
      layoutSize: "120%",
      itemStyle: {
        // normal: {
        areaColor: {
          type: "linear-gradient",
          x: 0,
          y: 1200,
          x2: 1000,
          y2: 0,
          colorStops: [
            {
              offset: 0,
              color: "#152E6E", // 0% 处的颜色
            },
            {
              offset: 1,
              color: "#0673AD", // 50% 处的颜色
            },
          ],
          global: true, // 缺省为 false
        },
        shadowColor: "#0f5d9d",
        shadowOffsetX: 0,
        shadowOffsetY: 15,
        opacity: 0.5,
        // },

      },
      emphasis: {
        areaColor: "#0f5d9d",
      },

      regions: [
        {
          name: "南海诸岛",
          itemStyle: {
            areaColor: "rgba(0, 10, 52, 1)",
            borderColor: "rgba(0, 10, 52, 1)",
            // normal: {
            opacity: 0,
            label: {
              show: false,
              color: "#009cc9",
            },
            // },
          },
          label: {
            show: false,
            color: "#FFFFFF",
            fontSize: 12,
          },
        },
      ],
    },
    series: [
      {
        type: "map",
        // selectedMode: "multiple",
        map: "china",
        aspectScale: 0.8,
        layoutCenter: ["50%", "50%"], //地图位置
        layoutSize: "120%",
        zoom: 1, //当前视角的缩放比例
        // roam: true, //是否开启平游或缩放
        scaleLimit: {
          //滚轮缩放的极限控制
          min: 1,
          max: 2,
        },
        label: {
          show: true,
          color: "#FFFFFF",
          fontSize: 16,
        },
        itemStyle: {
          // normal: {
          areaColor: "#0c3653",
          borderColor: "#1cccff",
          borderWidth: 1.8,
          // },
        },
        emphasis: {
          areaColor: "#56b1da",
          label: {
            show: true,
            color: "#fff",
          },
        },
        data: data,
      },
      {
        type: 'scatter',
        coordinateSystem: 'geo',
        //   symbol: 'image://http://ssq168.shupf.cn/data/biaoji.png',
        // symbolSize: [30,120],
        // symbolOffset:[0, '-40%'] ,
        symbol: 'pin',
        symbolSize: [45, 45],
        label: {
          // normal: {
          show: true,
          color: '#fff',
          formatter(value: any) {
            return value.data.value[2]
          }
          // }
        },
        itemStyle: {
          // normal: {
          color: '#D8BC37', //标志颜色
          // }
        },
        data: data,
      },
    ],
  })
  window.addEventListener('resize', function () { charts.resize(); });
  charts.on('click', (e: any) => {
    main.item = e.data.children
  })
}

const initPie = () => {
  const charts = echarts.init(document.querySelector('.box-left-pie') as HTMLElement)
  charts.setOption({
    tooltip: {
      trigger: 'item'
    },
    series: [
      {
        name: '新增',
        type: 'pie',
        radius: ['40%', '70%'],
        label: {
          show: true,
        },
        emphasis: {
          label: {
            show: true,
            fontSize: '20',
            fontWeight: 'bold'
          }
        },
        data: main.cityDetail.map((item: any) => {
          return {
            name: item.city,
            value: item.local_wzz_add
          }
        })
      }
    ]
  })
  window.addEventListener('resize', function () { charts.resize(); });
}
const initLine = () => {
  const charts = echarts.init(document.querySelector('.box-left-line') as HTMLElement)
  charts.setOption({
    xAxis: {
      type: 'category',
      data: main.cityDetail.map((item: any) => {
        return item.city
      })
    },
    yAxis: {
      type: 'value'
    },
    tooltip: {
      trigger: 'axis'
    },
    label: {
      show: true
    },
    series: [
      {
        data: main.cityDetail.map((item: any) => {
          return item.local_wzz_add
        }),
        type: 'line',
        smooth: true
      }
    ]
  })
  window.addEventListener('resize', function () { charts.resize(); });
}
</script>

<template>
  <div class="box">
    <div class="box-left">
      <div>
        <div>
          <div>较上日 {{ main.chinaAdd.localConfirmH5 }}</div>
          <div>{{ main.chinaTotal.localConfirm }}</div>
          <div>本土现有确诊</div>
        </div>
        <div>
          <div>较上日 {{ main.chinaAdd.nowConfirm }}</div>
          <div>{{ main.chinaTotal.nowConfirm }}</div>
          <div>现有确诊</div>
        </div>
        <div>
          <div>较上日 {{ main.chinaAdd.confirm }}</div>
          <div>{{ main.chinaTotal.confirm }}</div>
          <div>累计确诊</div>
        </div>
        <div>
          <div>较上日 {{ main.chinaAdd.noInfect }}</div>
          <div>{{ main.chinaTotal.noInfect }}</div>
          <div>无症状感染者</div>
        </div>
        <div>
          <div>较上日 {{ main.chinaAdd.importedCase }}</div>
          <div>{{ main.chinaTotal.importedCase }}</div>
          <div>境外输入</div>
        </div>
        <div>
          <div>较上日 {{ main.chinaAdd.dead }}</div>
          <div>{{ main.chinaTotal.dead }}</div>
          <div>累计死亡</div>
        </div>
      </div>
      <div class="box-left-pie"></div>
      <div class="box-left-line"></div>
    </div>
    <div class="box-center" id="China"></div>
    <div class="box-right">
      <table class="table" cellspacing="0" border="1">
        <thead>
          <th>地区</th>
          <th>新增确诊</th>
          <th>累计确诊</th>
          <th>治愈</th>
          <th>死亡</th>
        </thead>
        <tbody>
          <tr v-for="(item, index) in main.item" :key="index">
            <td align="center">{{ item.name }}</td>
            <td align="center">{{ item.today.confirm }}</td>
            <td align="center">{{ item.total.confirm }}</td>
            <td align="center">{{ item.total.heal }}</td>
            <td align="center">{{ item.total.dead }}</td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>

<style lang="less">
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

table {
  border-collapse: collapse;
  margin: 0 auto;
  text-align: center;
}

table td,
table th {
  border: 1px solid #cad9ea;
  color: #666;
  height: 30px;
}

table thead th {
  background-color: #CCE8EB;
  width: 100px;
}

table tr:nth-child(odd) {
  background: #fff;
}

table tr:nth-child(even) {
  background: #F5FAFA;
}

.box {
  display: flex;
  justify-content: space-between;

  &-left {
    width: 400px;

    &>div {
      display: flex;
      flex-wrap: wrap;

      &>div {
        width: 33.33%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        border: 1px solid black;
      }
    }

    &-pie {
      height: 40vh;
    }

    &-line {
      height: 40vh;
    }
  }

  &-center {
    flex: 1;
    height: 85vh;
  }

  &-right {
    width: 400px;
  }
}
</style>
